package com.meihua.shellapp.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.MainActivity;
import com.meihua.shellapp.adapter.ViewPagerAdapter;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.utils.ConstantUtils;
import com.meihua.shellapp.view.CircularImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：lc on 2017/10/25.
 * 邮箱：lc0607live@163.com
 */

public class GuideActivity extends BaseActivity {

    @Bind(R.id.ll_rounds)
    LinearLayout ll_rounds;//显示页标识
    @Bind(R.id.vp)
    ViewPager vp;//滑动页
    @Bind(R.id.tv_next)
    TextView tv_next;//立即进入


    private List<ImageView> vp_data = new ArrayList<>();
    private List<ImageView> views = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_guide;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        initData();
        tv_next.setOnClickListener(this);
        vp.setAdapter(new ViewPagerAdapter(vp_data));
        vp.addOnPageChangeListener(lis);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_next://立即进入
                startActivity(new Intent(mContext, MainActivity.class));
                finish();
                break;
        }
    }

    /**
     * 翻页监听事件
     */
    private ViewPager.OnPageChangeListener lis = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            for (int i = 0; i < views.size(); i++) {
                views.get(i).setImageResource(R.color.color_parting);
                if (i == position) {
                    views.get(i).setImageResource(R.color.white);
                }
            }
            if (position == vp_data.size() - 1) {
                ll_rounds.setVisibility(View.GONE);
                tv_next.setVisibility(View.VISIBLE);
            } else {
                ll_rounds.setVisibility(View.VISIBLE);
                tv_next.setVisibility(View.GONE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    /**
     * 初始化数据
     */
    private void initData() {
        addvpData(R.drawable.guide_1);
        addvpData(R.drawable.guide_2);
        addvpData(R.drawable.guide_3);
        addLLRounds();
    }

    /**
     * 为vp添加数据
     *
     * @param drawableResoure
     */
    private void addvpData(int drawableResoure) {
        ImageView img = new ImageView(this);
        img.setImageResource(drawableResoure);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        LinearLayout.LayoutParams limg = new LinearLayout.LayoutParams(ConstantUtils.getInstance().getScreenW(mContext),
                ConstantUtils.getInstance().getScreenH(mContext));
        img.setLayoutParams(limg);
        vp_data.add(img);//添加到vp数据
        CircularImage imground = new CircularImage(this);

        imground.setScaleType(ImageView.ScaleType.CENTER_CROP);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) getResources().getDimension(R.dimen.x34),
                (int) getResources().getDimension(R.dimen.x34));
        if (views.size() != 0) {
            imground.setImageResource(R.color.color_parting);
            lp.leftMargin = (int) getResources().getDimension(R.dimen.x88);
        } else if (views.size() == 0) {
            imground.setImageResource(R.color.white);
        }
        imground.setLayoutParams(lp);
        views.add(imground);//添加到提示点数据
    }

    /**
     * 添加view到ll_rounds
     */
    private void addLLRounds() {
        ll_rounds.removeAllViews();
        for (View temp : views) {
            ll_rounds.addView(temp);
        }
    }
}
